import os
import zipfile
import concurrent.futures
from tqdm import tqdm

def zip_folder(folder_path, directory_path):
    # 创建压缩文件名
    zip_file_name = f"{os.path.basename(folder_path)}.zip"
    zip_file_path = os.path.join(directory_path, zip_file_name)

    # 创建压缩文件
    with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
        total_files = sum([len(files) for _, _, files in os.walk(folder_path)])  # 计算文件总数
        with tqdm(total=total_files, desc=f"压缩 {os.path.basename(folder_path)}", unit='file', ncols=100) as pbar:
            # 遍历文件夹中的所有文件
            for root, _, files in os.walk(folder_path):
                for file in files:
                    file_path = os.path.join(root, file)
                    # 将文件添加到压缩包
                    zipf.write(file_path, os.path.relpath(file_path, directory_path))
                    pbar.update(1)  # 更新进度条

    print(f"已压缩: {folder_path} -> {zip_file_path}")

def zip_folders_in_directory(directory_path):
    # 获取所有第一级子文件夹
    subfolders = [os.path.join(directory_path, folder) for folder in os.listdir(directory_path)
                  if os.path.isdir(os.path.join(directory_path, folder))]

    # 使用ThreadPoolExecutor并发压缩
    with concurrent.futures.ThreadPoolExecutor() as executor:
        # 异步执行压缩任务
        executor.map(lambda folder: zip_folder(folder, directory_path), subfolders)

# 替换为你的路径A
directory_path = '/home/champrin/Desktop/record_data/网盘/兵种内录/a已上传/工程'
zip_folders_in_directory(directory_path)
